
package ysw.jdbc;
import ysw.dao.PayDao;
import ysw.databaseconnection.*;
import java.sql.*;
import ysw.vo.*;
import java.util.ArrayList;
import java.util.List;


public class PayJdbc implements PayDao{
    private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public PayJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }
public Pay addPay(Pay pay){
        String sql="insert into pays values(?,?,?,?)";
        try{
            psmt=this.getConn().prepareStatement(sql);
    psmt.setInt(1,pay.getPayid());
    psmt.setDouble(2,pay.getPaymoney());
    psmt.setDate(3,pay.getPaytime());
    psmt.setString(4,pay.getPaytype());
   
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return pay;
        }

public List showPay() throws SQLException{
    String sql="select * from pays";
    List payList=new ArrayList();
    try{
        psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
            Pay pay=new Pay();
            pay.setPayid(rs.getInt("payid"));
            pay.setPaymoney(rs.getDouble("paymoney"));
            pay.setPaytime(rs.getDate("paytime"));
            pay.setPaytype(rs.getString("paytype"));
        }
        return payList;
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
             if(rs!=null){
                   rs.close();
                   rs=null;
                }
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return payList;
} 

public Pay showOnePay(Integer payid){
    String sql="select * from pays where payid="+payid;
    Pay pay=new Pay();
    try{
      psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
        
           
            pay.setPayid(rs.getInt("payid"));
            pay.setPaymoney(rs.getDouble("paymoney"));
            pay.setPaytime(rs.getDate("paytime"));
            pay.setPaytype(rs.getString("paytype"));
        }
 
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            psmt.close();
              
            }catch(SQLException e){
                        e.printStackTrace();
                        }
        try{conn.close();
        
        }catch(SQLException e){
             e.printStackTrace();
        }
            }
            return pay;
}    

public void deletePay(Integer payid){
    String sql="delete from pays where payid="+payid;
    System.out.println(payid+"payjdbc");
    try{
        psmt=this.getConn().prepareStatement(sql);
        psmt.execute();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            psmt.close();
        }catch(SQLException e){
                e.printStackTrace();
        }
        try{
            conn.close();
        }catch(SQLException e){
                e.printStackTrace();
        }
    }
}  
public Pay updateSavePay(Pay pay){
    String sql="update pays set payid=?,paymoney=?,paytime=?,paytype=? where payid="+pay.getPayid();
     try{
            psmt=this.getConn().prepareStatement(sql);
            pay.setPayid(rs.getInt("payid"));
            pay.setPaymoney(rs.getDouble("paymoney"));
            pay.setPaytime(rs.getDate("paytime"));
            pay.setPaytype(rs.getString("paytype"));
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
         try{
             psmt.close();
            
         }catch(SQLException e){
             e.printStackTrace();
         }
     }
     return pay;
}
}
